package com.client;

import com.entity.Request;
import com.entity.Response;
import java.io.*;
import java.net.InetSocketAddress;
import java.net.Socket;

public class Client {

    public String host_account;
    public String iP;
    public Socket socket;

    public Client(String host_account, String iP){
        this.host_account = host_account;
        this.iP = iP;

        socket = new Socket();

        try {
            socket.connect(new InetSocketAddress(iP,6666));
            System.out.println("连接服务端成功 连接账户"+host_account+"    ip地址："+iP);

            try {
                InputStream is= socket.getInputStream();
                ObjectInputStream ois=new ObjectInputStream(is);
                while (true) {
                    Response res=(Response)ois.readObject();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            System.out.println("连接服务端失败 连接账户"+host_account+"    ip地址："+iP);
            e.printStackTrace();
        }
    }

    public void send(Request request){
        OutputStream os = null;
        try {
            os = socket.getOutputStream();
            ObjectOutputStream oos=new ObjectOutputStream(os);
            oos.writeObject(request);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
